{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "前6道题，自己独立完成\n",
    "后面题，跟着视频学习后，默写下来的\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "单位转换\n",
    "\"\"\"\n",
    "cm = int(input(\"input cm:\"))\n",
    "\n",
    "foot = int(cm/100 / 0.3048)\n",
    "inch  = int((cm/(100*0.3048) - foot) * 12) \n",
    "\n",
    "if foot > 1:\n",
    "\tfoot_unit = \"feet\"\n",
    "else:\n",
    "\tfoot_unit = \"foot\"\n",
    "\n",
    "if inch > 1:\n",
    "\tinch_unit = \"inches\"\n",
    "else:\n",
    "\tinch_unit = \"inch\"\n",
    "\n",
    "print(foot,foot_unit,inch,inch_unit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 "
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "输出质数\n",
    "\"\"\"\n",
    "for i in range(10,100):\n",
    "    if i % 2 == 0 :\n",
    "    \tprint(i, end = ' ')\t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "打印九九乘法表\n",
    "\"\"\"\n",
    "def print99():\n",
    "        for i in range(1, 10):\n",
    "            for j in range(1, i+1):\n",
    "                print('%d * %d =%2s ' %(j,i,i*j)),\n",
    "            print('\\n')\n",
    "\n",
    "print99()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "字符串切分\n",
    "\"\"\"\n",
    "string = \" A a236 E  GHSshg  A  \"\n",
    "\n",
    "s = string.split()\n",
    "# print(s)\n",
    "\n",
    "del s[0];del s[-1]\n",
    "print(s)\n",
    "a = len(s)\n",
    "print(len(s))\n",
    "print(type(s))\n",
    "\n",
    "# for遍历list\n",
    "for i in s:\n",
    " \tprint(i)\n",
    "\n",
    "# 利用python内置函数enumerate（）列举出list中的数\n",
    "for i in enumerate(s):\n",
    "\tprint(i)\n",
    "\n",
    "# 使用iter()\n",
    "for i in iter(s):\n",
    "\tprint(i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "字符串索引\n",
    "\"\"\"\n",
    "string = 'abcdefghijklmno'\n",
    "a = list(string)\n",
    "print(a)\n",
    "'''\n",
    "index从0开始\n",
    "\n",
    "'''\n",
    "j = 0\n",
    "\n",
    "for i in a: # 遍历列表\n",
    "\t\n",
    "\tif j % 2 != 0: # 判断index是否是质数\n",
    "\t\tprint(i)\n",
    "\tj += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "list \n",
    "question 0 :列表的定义\n",
    "\"\"\"\n",
    "L = list(range(1,11))\n",
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180, 198]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k =  [x*2 for x in range(100) if x % 9 == 0]\n",
    "k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1,\n",
       " 2,\n",
       " 3,\n",
       " 4,\n",
       " 5,\n",
       " 6,\n",
       " 7,\n",
       " 8,\n",
       " 9,\n",
       " 10,\n",
       " 0,\n",
       " 18,\n",
       " 36,\n",
       " 54,\n",
       " 72,\n",
       " 90,\n",
       " 108,\n",
       " 126,\n",
       " 144,\n",
       " 162,\n",
       " 180,\n",
       " 198,\n",
       " 0,\n",
       " 18,\n",
       " 36,\n",
       " 54,\n",
       " 72,\n",
       " 90,\n",
       " 108,\n",
       " 126,\n",
       " 144,\n",
       " 162,\n",
       " 180,\n",
       " 198]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lk = L + k\n",
    "lk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 5 6 7 8 9 10 0 18 36 54 72 90 108 126 144 162 180 198 0 18 36 54 72 90 108 126 144 162 180 198 "
     ]
    }
   ],
   "source": [
    "for i in lk:\n",
    "        print(i, sep = ' ',end = ' ')\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 0   0   0   0   0   0   0   0   0   0\n",
      " 0   1   2   3   4   5   6   7   8   9\n",
      " 0   2   4   6   8  10  12  14  16  18\n",
      " 0   3   6   9  12  15  18  21  24  27\n",
      " 0   4   8  12  16  20  24  28  32  36\n",
      " 0   5  10  15  20  25  30  35  40  45\n",
      " 0   6  12  18  24  30  36  42  48  54\n",
      " 0   7  14  21  28  35  42  49  56  63\n",
      " 0   8  16  24  32  40  48  56  64  72\n",
      " 0   9  18  27  36  45  54  63  72  81\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "question 1:列表的嵌套\n",
    "\"\"\"\n",
    "matrix = []\n",
    "for i in range(10):\n",
    "    row = [ ]\n",
    "    for j in range(10):\n",
    "        row.append(i*j)\n",
    "    matrix.append(row)\n",
    "    \n",
    "for i in range(len(matrix)):\n",
    "#     print(matrix[i])\n",
    "    print(\"  \".join([\"{:>2}\".format(x) for x in matrix[i]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A B C D E \n",
      "1 2 3 4 5 \n",
      "A => 1\n",
      "B => 2\n",
      "C => 3\n",
      "D => 4\n",
      "E => 5\n",
      "dict_items([('A', 1), ('B', 2), ('C', 3), ('D', 4), ('E', 5)])\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "字典\n",
    "question 0:字典定义\n",
    "\"\"\"\n",
    "dic = {'A':1,'B':2,'C':3,'D':4,'E':5}\n",
    "keys = dic.keys()\n",
    "for key in keys:\n",
    "    print(key, end = ' ')\n",
    "print()\n",
    "for key in keys:\n",
    "    print(dic[key], end = ' ')\n",
    "print()\n",
    "\n",
    "for key, value in dic.items():\n",
    "    print(key, \"=>\", value)\n",
    "print(dic.items())  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "E -> 5\n",
      "D -> 4\n",
      "C -> 3\n",
      "B -> 2\n",
      "A -> 1\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "question 1:字典的排序\n",
    "\"\"\"\n",
    "dic = {'A':1,'B':2,'C':3,'D':4,'E':5}\n",
    "dic2 = dict()\n",
    "\n",
    "for key ,value in sorted(dic.items(), key = lambda x:x[1], reverse=True):\n",
    "    print(key,\"->\", value)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alice, good day!\n",
      "long name\n",
      "Ana, good day!\n",
      "short name\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "函数定义\n",
    "\"\"\"\n",
    "def hello(name):\n",
    "    print(name + \", good day!\")\n",
    "    if len(name) > 3:\n",
    "        return \"Yes\"\n",
    "    else:\n",
    "        return \"No\"\n",
    "\n",
    "ret = hello(\"alice\")\n",
    "if ret == \"Yes\":\n",
    "    print(\"long name\")\n",
    "else:\n",
    "    print(\"short name\")\n",
    "ret = hello(\"Ana\")\n",
    "if ret == \"Yes\":\n",
    "    print(\"long name\")\n",
    "else:\n",
    "    print(\"short name\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 "
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "函数----递归\n",
    "\"\"\"\n",
    "def fibonacci(n):\n",
    "    if n == 1 or n == 2:\n",
    "        return 1\n",
    "    if n > 2:\n",
    "        return fibonacci(n-1) + fibonacci(n-2)\n",
    "    if n < 1 :\n",
    "        print(\"Wrong\")\n",
    "# fibonacci(5)\n",
    "\n",
    "for i in range(1,21):\n",
    "    print(fibonacci(i), end = ' ')\n",
    "    \n",
    "    \n",
    "fibonacci(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice  good day !\n",
      "Ana  good day !\n",
      "Neo  good day !\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "全局变量\n",
    "\"\"\"\n",
    "def greet(name:str) -> None:  # :str -> None 仅说明作用\n",
    "    global count \n",
    "    count += 1\n",
    "    print(name, \" good day !\")\n",
    "\n",
    "def main():\n",
    "    greet(\"Alice\")\n",
    "    greet(\"Ana\")\n",
    "    greet(\"Neo\")\n",
    "    global count\n",
    "    print(count)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    count = 0\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
